//
//  XRecordViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/21.
//

#import "XRecordViewController.h"
#import "XDynamicRecordTableViewCell.h"
#import <LYEmptyView/LYEmptyView.h>
#import <LYEmptyView/UIView+Empty.h>

#import "XDynamicModel.h"
#import "RFaceCheckRequest.h"
#import "XRcordDetailsViewController.h"
#import "XRcordEditViewController.h"

@interface XRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)MyRelativeLayout* rootLayout;

@property(nonatomic,strong)UITableView* tableView;

@property(nonatomic,strong)UIButton* addBtn;


@property(nonatomic,strong)RLMResults<XDynamicModel*> *datas;

@end

@implementation XRecordViewController

- (void)loadView{
    [super loadView];
    
    self.view.backgroundColor = COLOR_WHITE;
    
    MyRelativeLayout* rootLayout = [MyRelativeLayout new];
    rootLayout.myMargin = 0;
    [self.view addSubview:rootLayout];
    _rootLayout = rootLayout;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupTableView];
}


-(void)setupTableView{
    
    //列表
    UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.backgroundColor = COLOR_BACKGEOUND;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.estimatedRowHeight = PX(100);
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.sectionFooterHeight = CGFLOAT_MIN;
    tableView.sectionHeaderHeight = 10;
    tableView.tableFooterView = [UIView new];
    [tableView registerClass:NSClassFromString(dynamicRecordCell) forCellReuseIdentifier:dynamicRecordCell];
    tableView.myMargin = 0;
    [self.rootLayout addSubview:tableView];
    _tableView = tableView;
    
    LYEmptyView* emptyView = [LYEmptyView emptyViewWithImageStr:NULL titleStr:@"快乐生活每一天" detailStr:@"快来记录每天不一样的自己"];
    emptyView.titleLabFont = FONT_FIT(12);
    emptyView.titleLabTextColor = COLOR_BLACK_DARK;
    emptyView.detailLabFont = FONT_FIT(12);
    emptyView.detailLabTextColor = COLOR_GRAY_BASE;
    emptyView.detailLabMargin = 10;
    emptyView.contentViewOffset = -100;
    tableView.ly_emptyView = emptyView;
    
    
    UIButton* addBtn = [UIButton new];
    [addBtn setImage:[UIImage imageNamed:@"x_dynamic_add"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addRecord:) forControlEvents:UIControlEventTouchUpInside];
    addBtn.myBottom = 8;
    addBtn.myRight = 0;
    [addBtn sizeToFit];
    [self.rootLayout addSubview:addBtn];
}

#pragma mark -- reloadData
// 刷新数据
- (void)reloadData{
    RLMResults<XDynamicModel*> *models = [XDynamicModel objectsWhere:@"type = 1"];
    self.datas = models;
    [self.tableView reloadData];
}

- (void)addRecord:(UIButton*)sender{
    XRcordEditViewController* editController = [XRcordEditViewController new];
    editController.operation = EditOperation_Add;
    editController.editBlock = ^(id  _Nullable obj, EditOperation operation) {
        [self reloadData];
    };
    [UIViewController showViewController:editController animated:YES];
}

#pragma mark --UITableViewDelegate  UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XDynamicRecordTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:dynamicRecordCell forIndexPath:indexPath];
    cell.dynamic = [self.datas objectAtIndex:indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XRcordDetailsViewController* detailsController = [XRcordDetailsViewController new];
    detailsController.record = [self.datas objectAtIndex:indexPath.section];
    detailsController.editBlock = ^(id  _Nullable obj, EditOperation operation) {
        [self reloadData];
    };
    [UIViewController showViewController:detailsController animated:YES];
}

#pragma mark -- check

-(void)requestData{
    //getEtc 3-a 4-b
    RFaceCheckRequest* request = [RFaceCheckRequest new];
    [RequestOperation request:request quite:YES callBack:^(HResponse * _Nullable response) {
        [response success:^(id  _Nonnull data) {
            NSArray* list = [NSArray yy_modelArrayWithClass:XDynamicModel.class json:data];
            if(list.count == 4){
                [[NSNotificationCenter defaultCenter] postNotificationName:kChangeNotification object:NULL userInfo:NULL];
            }else{
                [self reloadData];
            }
        }];
    }];
}
@end

